﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;

namespace TwitterFollowersMonitor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool isFirstInstance = true;
            Mutex m = new Mutex(true, "KRASIS_TwitterFollowersMonitor", out isFirstInstance);
            if (isFirstInstance)
            {
                try
                {
                    //Detect changes in Windows user session state for notifications
                    SystemEvents.SessionSwitch += new SessionSwitchEventHandler(GlobalExceptionHandler.OnSessionSwitch);

                    //Handle all the possible unhandled exceptios
                    Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionHandler.UIUnhandledException);
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GlobalExceptionHandler.NonUIUnhandledException);
                    
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new frmMain());

                    //Needed for the mutex to be kept
                    GC.KeepAlive(m);
                }
                catch { }
                finally
                {
                    //Release mutex
                    try { m.ReleaseMutex(); }
                    catch { }
                }
            }
        }
    }
}
